package com.ego.dubbo.service.impl;

import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.ego.commons.exception.DaoException;
import com.ego.dubbo.service.TbContentDubboService;
import com.ego.mapper.TbContentMapper;
import com.ego.pojo.TbContent;
import com.ego.pojo.TbContentExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@DubboService
public class TbContentDubboServiceImpl implements TbContentDubboService {
    @Autowired
    private TbContentMapper tbContentMapper;

    @Override
    public PageInfo<TbContent> contentQueryList(long categoryId, int page, int size) {

        PageHelper.startPage(page,size);

        TbContentExample tbContentExample = new TbContentExample();
        if (categoryId!=0L){
            tbContentExample.createCriteria().andCategoryIdEqualTo(categoryId);
        }

        List<TbContent> tbContents = tbContentMapper.selectByExampleWithBLOBs(tbContentExample);
        System.out.println("tbContents = " + tbContents);
        PageInfo<TbContent> contentPageInfo=new PageInfo<>(tbContents);

        return contentPageInfo;
    }

    @Override
    public int insert(TbContent tbContent) {
        return tbContentMapper.insertSelective(tbContent);
    }

    @Override
    public int update(TbContent tbContent) {
        return tbContentMapper.updateByPrimaryKeySelective(tbContent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(List<Long> ids) throws DaoException {
        TbContentExample tbContentExample = new TbContentExample();
        tbContentExample.createCriteria().andIdIn(ids);

            int i = tbContentMapper.deleteByExample(tbContentExample);
            if (ids.size()==i){
                return 1;
            }else {
            throw  new DaoException("批量删除失败");}
    }

    @Override
    public List<TbContent> selectAllByCategoryid(Long categoryId) {
        TbContentExample tbContentExample = new TbContentExample();
        tbContentExample.createCriteria().andCategoryIdEqualTo(categoryId);

        List<TbContent> tbContents = tbContentMapper.selectByExampleWithBLOBs(tbContentExample);
        return tbContents;
    }

    @Override
    public List<TbContent> selectByIds(List<Long> ids) {
        TbContentExample tbContentExample = new TbContentExample();
        tbContentExample.createCriteria().andIdIn(ids);

        List<TbContent> tbContents = tbContentMapper.selectByExampleWithBLOBs(tbContentExample);


        return tbContents;
    }
}